﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using FlyHorse.LIMS.Model;
using FlyHorse.LIMS.WinApp.LanguageResource;

namespace FlyHorse.LIMS.WinApp.BaseManageForms.GoodsForms
{
    public partial class GoodsMainForm : DevExpress.XtraEditors.XtraForm
    {
        BLL.GoodBll goodsBll = new BLL.GoodBll();
        BLL.ToolBll toolBll = new BLL.ToolBll();
        public GoodsMainForm()
        {
            InitializeComponent();
        }

        private void GoodsMainForm_Load(object sender, EventArgs e)
        {
            this.gridView1.FocusRectStyle = DevExpress.XtraGrid.Views.Grid.DrawFocusRectStyle.RowFocus;
            this.gridView1.OptionsBehavior.Editable = false;
            this.gridView1.OptionsSelection.EnableAppearanceFocusedCell = false;

            //绑定grid里的样品类型
            var goodsTypeList = toolBll.GetGoodsTypeList();
            this.repositoryItemLookUpEdit1.DataSource = goodsTypeList;

            InitGrid();
        }

        private void InitGrid()
        {
            var models = goodsBll.LoadEntities(m => true).OrderByDescending(m=>m.GoodsTypeID).ThenBy(m=>m.GoodsName).ToList();
            this.gridControl1.DataSource = models;
        }

        private void ButtonItem_add_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            GoodsAddForm goodsAddForm = new GoodsAddForm(InitGrid);
            goodsAddForm.Show();
        }

        //private bool AddGoods(Goods goods)
        //{
        //    int count = goodsBll.LoadEntities(m => m.GoodsName == goods.GoodsName && m.IsDelete == false && m.GoodsTypeID == goods.GoodsTypeID).Count();
        //    if (count > 0)
        //    {
        //        XtraMessageBox.Show("该分类下已经存在相同的样品名称了！");
        //        return false;
        //    }

        //    Goods res = goodsBll.AddEntity(goods);
        //    if (res != null)
        //    {
        //        InitGrid();
        //        XtraMessageBox.Show("添加成功!");
        //        return true;
        //    }
        //    else
        //    {
        //        XtraMessageBox.Show("添加失败!");
        //        return false;
        //    }

        //}

        private void ButtonItem_Delete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (this.gridView1.GetFocusedRowCellValue("GoodsID")==null)
            {
                XtraMessageBox.Show(ResourceOverall.SelectNull);
                return;
            }
            using (OJCB4Entities db =new OJCB4Entities())
            {
                string id = Convert.ToString(this.gridView1.GetFocusedRowCellValue("GoodsID"));
                Good goods = db.Goods.Where(m => m.GoodsID == id).First();
                int count = toolBll.GetReportCountByGoodsID(goods);
                if (count > 0)
                {
                    XtraMessageBox.Show(ResourceOverall.DeleteExists);
                    return;
                }
                if (count == -1)
                {
                    XtraMessageBox.Show(ResourceOverall.SystemError);
                    return;
                }

                DialogResult result = XtraMessageBox.Show(ResourceOverall.ConfirmDelete, ResourceOverall.SystemWarningStr, MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                if (result == DialogResult.OK)
                {
                    db.Goods.Remove(goods);
                    bool res = db.SaveChanges()>0;
                    if (res)
                    {
                        InitGrid();
                        XtraMessageBox.Show(ResourceOverall.DeleteSucceed);
                    }
                    else
                    {
                        XtraMessageBox.Show(ResourceOverall.DeleteFailed);
                    }

                }
            }
        }

        private void ButtonItem_Edit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (this.gridView1.GetFocusedRowCellValue("GoodsID") == null)
            {
                XtraMessageBox.Show(ResourceOverall.SelectNull);
                return;
            }
            string id = Convert.ToString(this.gridView1.GetFocusedRowCellValue("GoodsID"));
            Good goods = goodsBll.LoadEntities(m => m.GoodsID == id).First();
         

            DialogResult result = XtraMessageBox.Show(ResourceOverall.ConfirmEdit, ResourceOverall.SystemWarningStr, MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
            if (result == DialogResult.OK)
            {
                GoodsAddForm form = new GoodsAddForm(goods, InitGrid);
                form.Show();
            }
        }


    }
}